package com.winai.launcher.BroadcastReceiver;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.text.TextUtils;

import com.ks.projectbasictools.utils.LogUtils;
import com.winai.launcher.common.GlobalPublisher;
import com.winai.launcher.constant.Constant;
import com.winai.launcher.override.AlarmCountTimer;
import com.winai.launcher.override.BluetoothCountTimerObserver;

import java.lang.reflect.Method;

/**
 * 作者：康少
 * 时间：2020/4/30 0030
 * 说明：蓝牙广播
 */
public class BluetoothReceiver extends BroadcastReceiver {
    private final String TAG = "BluetoothReceiver";

    public void register(Context mContext) {
        /**
         * 异步搜索蓝牙设备——广播接收
         */
        IntentFilter filter = new IntentFilter();
        filter.addAction(BluetoothDevice.ACTION_FOUND);// 找到设备的广播
        filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);// 监视蓝牙关闭和打开的状态
        filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);// `搜索开始的广播
        filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);// 搜索完成的广播
        filter.addAction(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED);//连接状态改变广播
        filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);//连接成功广播
        filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);//连接断开广播
        filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);//设备配对状态改变
        // 注册广播
        mContext.getApplicationContext().registerReceiver(BluetoothReceiver.this, filter);
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        // 收到的广播类型
        String action = intent.getAction();
        // 从intent中获取设备
        BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
        switch (action) {
            case BluetoothAdapter.ACTION_DISCOVERY_STARTED://设备搜索开启
                LogUtils.i(TAG + "：搜索开启");
                notifyReceiver(context, Constant.ACTION_BLUETOOTH_SCAN_START, device);
                break;
            case BluetoothDevice.ACTION_FOUND:// 发现设备的广播
                if (device != null) {
                    if (!TextUtils.isEmpty(device.getName())) {
                        LogUtils.i(TAG + "：蓝牙名称：" + device.getName() + "，物理地址：" + device.getAddress());
                    }
                }
                notifyReceiver(context, Constant.ACTION_BLUETOOTH_FIND_ONE, device);
                break;
            case BluetoothAdapter.ACTION_DISCOVERY_FINISHED:// 设备搜索完成
                LogUtils.i(TAG + "：搜索完成");
                notifyReceiver(context, Constant.ACTION_BLUETOOTH_SCAN_COMPLETE, device);
                break;
            case BluetoothDevice.ACTION_ACL_CONNECTED:
                LogUtils.i(TAG + "：蓝牙设备已连接 " + device.getName());
                Constant.operationDevice = device;
                Constant.bluetoothLinked = true;
                notifyReceiver(context, Constant.ACTION_BLUETOOTH_DEVICE_CONNECTED, device);
                break;
            case BluetoothDevice.ACTION_ACL_DISCONNECTED:
                LogUtils.i(TAG + "：蓝牙设备已断开 " + device.getName());
                Constant.operationDevice = device;
                Constant.bluetoothLinked = false;
                notifyReceiver(context, Constant.ACTION_BLUETOOTH_DEVICE_DISCONNECTED, device);
                break;
            /**
             * 设备配对状态改变
             * int BOND_NONE = 10; //配对没有成功
             * int BOND_BONDING = 11; //配对中
             * int BOND_BONDED = 12; //配对成功
             */
            case BluetoothDevice.ACTION_BOND_STATE_CHANGED:
                LogUtils.i(TAG + "：设备配对状态改变：" + device.getName() + "，" + device.getBondState());
                notifyReceiver(context, Constant.ACTION_BLUETOOTH_BOND_STATE_CHANGED, device);
                break;
            case BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED:
                int state = intent.getIntExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE, 0);
                LogUtils.i(TAG + "：蓝牙设备连接状态改变 STATE: " + state);
                if (state == 0) {
                    Constant.operationDevice = device;
                    Constant.bluetoothLinked = false;
                    notifyReceiver(context, Constant.ACTION_BLUETOOTH_DEVICE_DISCONNECTED, device);
                } else if (state == 1) {
                    Constant.operationDevice = device;
                    Constant.bluetoothLinked = false;
                    notifyReceiver(context, Constant.ACTION_BLUETOOTH_DEVICE_CONNECTING, device);
                } else if (state == 2) {
                    Constant.operationDevice = device;
                    Constant.bluetoothLinked = true;
                    notifyReceiver(context, Constant.ACTION_BLUETOOTH_DEVICE_CONNECTED, device);
                    BluetoothCountTimerObserver.getInstance().stopTimer();//蓝牙倒计时——停止
                }
                break;
            case BluetoothAdapter.ACTION_STATE_CHANGED:
                int blueState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 0);
                switch (blueState) {
                    case BluetoothAdapter.STATE_TURNING_ON:
//                            ToastUtil.show(mContext, "正在打开蓝牙");
                        break;
                    case BluetoothAdapter.STATE_ON:
                        LogUtils.i(TAG + "：蓝牙已打开");
                        notifyReceiver(context, Constant.ACTION_BLUETOOTH_STATE_ON, device);
                        GlobalPublisher.Notify(GlobalPublisher.BLUETOOTH_STATE_ON, device);
                        BluetoothCountTimerObserver.getInstance().startTimer();//蓝牙倒计时——开始
                        break;
                    case BluetoothAdapter.STATE_TURNING_OFF:
//                            ToastUtil.show(mContext, "正在关闭蓝牙");
                        break;
                    case BluetoothAdapter.STATE_OFF:
                        LogUtils.i(TAG + "：蓝牙已关闭");
                        Constant.bluetoothLinked = false;
                        Constant.operationDevice = null;
                        BluetoothCountTimerObserver.getInstance().stopTimer();//蓝牙倒计时——停止
                        notifyReceiver(context, Constant.ACTION_BLUETOOTH_STATE_OFF, device);
                        GlobalPublisher.Notify(GlobalPublisher.BLUETOOTH_STATE_OFF, device);
                        break;
                }
                break;
            default:
                break;
        }
    }

    private void notifyReceiver(Context mContext,String action, BluetoothDevice device) {
        Intent intent = new Intent();
        intent.setAction(action);
        intent.putExtra("device", device);
        mContext.sendBroadcast(intent);
    }
}
